using SmartLink.Domain.IServices;
using SmartLink.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SmartLink.Domain.Entity;
using SmartLink.DTOS;
using SmartLink.Domain.ExpandEntity;
using SmartLink.EnumLibrary;

namespace SmartLink.Domain.Services
{
    /// <summary>
    /// 系统功能模块业务逻辑
    /// </summary>
    public class SysModuleServices : ISysModuleServices
    {
        private ISYS_ModuleRepo _moduleRepo;
        private ISYS_ModuleFunctionRepo _moduleFunctionRepo;

        public SysModuleServices(ISYS_ModuleRepo moduleRepo,
            ISYS_ModuleFunctionRepo moduleFunctionRepo)
        {
            _moduleRepo = moduleRepo;
            _moduleFunctionRepo = moduleFunctionRepo;
        }

        #region 模块操作

        public OperationResult AddModule(SYS_Module entity)
        {
            var exist = _moduleRepo.Exist(entity.Code);
            if (exist)
                return new OperationResult(OperationResultType.ParamError, "模块编码已存在。");
            var id = _moduleRepo.Add(entity);
            return new OperationResult(OperationResultType.Success);
        }

        public OperationResult UpdateModule(SYS_Module entity)
        {
            var repeat = _moduleRepo.Repeat(entity.ID, entity.Code);
            if (repeat)
                return new OperationResult(OperationResultType.ParamError, "新的模块编码已存在。");
            var success = _moduleRepo.Update(entity);
            if (success)
                return new OperationResult(OperationResultType.Success);
            else
                return new OperationResult(OperationResultType.Error);
        }

        public OperationResult DeleteModule(int id)
        {
            var existChildren = _moduleRepo.ExistChildren(id);
            if (existChildren)
                return new OperationResult(OperationResultType.IllegalOperation, "存在子节点，删除失败。");
            var success = _moduleRepo.DeleteLogic(id);
            if (success)
                return new OperationResult(OperationResultType.Success);
            else
                return new OperationResult(OperationResultType.Error);
        }

        public SYS_Module GetModule(int id)
        {
            var entity = _moduleRepo.GetById(id);
            return entity;
        }

        public IEnumerable<SYS_Module> GetAllModule()
        {
            var entities = _moduleRepo.GetAll(false);
            return entities;
        }

        public IEnumerable<SysModuleExpand> GetAllWithRelation()
        {
            var expand = _moduleRepo.GetAllWithRelation(false);
            //排除超管独有模块
            var saOnlyIds = expand.Where(a => SuperAdminOnlyModule.ModulesStrArr.Contains(a.Code)).Select(a => a.ID);
            if (saOnlyIds.Any())
            {
                var idsList = new List<int>(saOnlyIds);
                foreach (var exId in saOnlyIds)
                {
                    var excludeIds = expand.Where(a => !(string.IsNullOrEmpty(a.Path)) && Array.ConvertAll(a.Path.Split(','), s => int.Parse(s)).Contains(exId)).Select(a => a.ID);
                    idsList.AddRange(excludeIds);
                }
                var ids = new HashSet<int>(idsList);
                expand = expand.Where(a => !ids.Contains(a.ID));
            }
            return expand;
        }

        public IEnumerable<SYS_Module> GetModuleChildren(int parenntId)
        {
            var entities = _moduleRepo.GetImmediateChildren(parenntId);
            return entities;
        }

        #endregion

        #region 模块功能项操作

        public OperationResult AddFunction(SYS_ModuleFunction entity)
        {
            var exist = _moduleFunctionRepo.Exist(entity.ModuleCode, entity.Code);
            if (exist)
                return new OperationResult(OperationResultType.ParamError, "功能编码在指定模块下已存在。");
            var id = _moduleFunctionRepo.Add(entity);
            return new OperationResult(OperationResultType.Success);
        }

        public OperationResult UpdateFunction(SYS_ModuleFunction entity)
        {
            var repeat = _moduleFunctionRepo.Repeat(entity.ID, entity.ModuleCode, entity.Code);
            if (repeat)
                return new OperationResult(OperationResultType.ParamError, "新的功能编码在指定模块下已存在。");
            var success = _moduleFunctionRepo.Update(entity);
            if (success)
                return new OperationResult(OperationResultType.Success);
            else
                return new OperationResult(OperationResultType.Error);
        }

        public OperationResult DeleteFunction(int id)
        {
            var success = _moduleFunctionRepo.DeleteLogic(id);
            if (success)
                return new OperationResult(OperationResultType.Success);
            else
                return new OperationResult(OperationResultType.Error);
        }

        public SYS_ModuleFunction GetFunction(int id)
        {
            var entity = _moduleFunctionRepo.GetById(id);
            return entity;
        }

        public IEnumerable<SYS_ModuleFunction> GetModuleFunctions(string moduleCode)
        {
            var entities = _moduleFunctionRepo.GetList(moduleCode);
            return entities.OrderBy(a => a.Sort);
        }

        public IEnumerable<SYS_ModuleFunction> GetAllFunction()
        {
            var entities = _moduleFunctionRepo.GetAll(false);
            return entities;
        }

        #endregion


    }
}
